package com.songlanyun.modules.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.songlanyun.modules.article.model.dto.ArticleDTO;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.Query;

import com.songlanyun.modules.article.dao.ArticleContentDao;
import com.songlanyun.modules.article.entity.ArticleContent;
import com.songlanyun.modules.article.service.ArticleContentService;


@Service("articleContentService")
public class ArticleContentServiceImpl extends ServiceImpl<ArticleContentDao, ArticleContent> implements ArticleContentService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ArticleContent> page = this.page(
                new Query<ArticleContent>().getPage(params),
                new QueryWrapper<ArticleContent>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveContent(Long aid, ArticleDTO dto) {
        ArticleContent articleContent = new ArticleContent(aid, dto.getContent(), dto.getMediaAddress(), dto.getMediaSize(), dto.getDuration());
        this.baseMapper.insert(articleContent);
    }

    @Override
    public void updateContent(Long aid, ArticleDTO dto) {
        ArticleContent articleContent = this.baseMapper.selectOne(
                new LambdaQueryWrapper<ArticleContent>()
                        .eq(ArticleContent::getArticleId, aid)
                        .last("limit 1")
        );

        articleContent.setContent(dto.getContent());
        articleContent.setMediaAddress(dto.getMediaAddress());
        articleContent.setMediaSize(dto.getMediaSize());
        articleContent.setDuration(dto.getDuration());
        this.baseMapper.updateById(articleContent);

    }

    @Override
    public ArticleContent getByAid(Long aid) {
        ArticleContent articleContent = this.baseMapper.selectOne(
                new LambdaQueryWrapper<ArticleContent>()
                        .eq(ArticleContent::getArticleId, aid)
                        .last("limit 1")
        );

       return articleContent;

    }

    @Override
    public void removeByAid(Long aid) {
        this.baseMapper.delete(
                new LambdaQueryWrapper<ArticleContent>()
                        .eq(ArticleContent::getArticleId, aid)
        );
    }

}